<template>
  <div class="manage">
    <el-dialog :title="operateType === 'edit' ? '编辑用户' : '新增用户'" :visible.sync="isShow">
      <common-form :formLabel="operateLabel" :form="operateForm"></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 添加</el-button>
      <common-form inline :formLabel="formLabel" :form="searchForm">
        <el-button type="primary" @click="search">查询</el-button>
      </common-form>
    </div>
    <common-table :tableData="tableData" :tableLabel="tableLabel" :config="config" @changePage="changePage" @edit="edituser" @del="delUser"></common-table>
  </div>
</template>

<script>
import CommonForm from '@/components/CommonForm'
import CommonTable from '@/components/CommonTable'
export default {
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      url: '',
      isShow: false,
      operateType: 'add',
      operateForm: {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',
      },
      operateLabel: [
        {
          model: 'name',
          label: '姓名',
        },
        {
          model: 'age',
          label: '年龄',
        },
        {
          model: 'sex',
          label: '性别',
          type: 'select',
          opts: [
            {
              label: '男',
              value: 1,
            },
            {
              label: '女',
              value: 0,
            },
          ],
        },
        {
          model: 'birth',
          label: '出生日期',
          type: 'date',
        },
        {
          model: 'addr',
          label: '地址',
        },
      ],
      tableData: [],
      tableLabel: [
        {
          prop: 'name',
          label: '姓名',
        },
        {
          prop: 'age',
          label: '年龄',
        },
        {
          prop: 'sexLabel',
          label: '性别',
        },
        {
          prop: 'birth',
          label: '出生日期',
          width: 120,
        },
        {
          prop: 'addr',
          label: '地址',
          width: 300,
        },
      ],
      config: {
        page: 1,
        total: 30,
        loading: false,
      },
      searchForm: {
        keyword: '',
      },
      formLabel: [
        {
          model: 'keyword',
          label: '',
        },
      ],
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList(name = '') {
      this.config.loading = true
      this.$http
        .get('/api/user/getUser', {
          params: {
            page: this.config.page,
            name,
          },
        })
        .then((res) => {
          console.log(res)
          this.tableData = res.data.list.map((item) => {
            item.sexLabel = item.sex === 0 ? '女' : '男'
            return item
          })
          this.config.total = res.data.count
          this.config.loading = false
        })
    },
    addUser() {
      this.isShow = true
      this.operateType = 'add'
      this.operateForm = {}
    },
    edituser(row) {
      this.isShow = true
      this.operateType = 'edit'
      this.operateForm = row
    },
    confirm() {
      if (this.operateType === 'add') {
        this.url = 'api/user/add'
      } else {
        this.url = 'api/user/edit'
      }
      this.$http.post(this.url, this.operateForm).then((res) => {
        this.$message({
          message: res.data.data.message,
          type: 'success',
        })
        this.getList()
        this.isShow = false
      })
    },
    delUser(row) {
      // console.log(row)
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          let id = row.id
          this.$http.get('/api/user/del', { params: { id } }).then((res) => {
            console.log(res)
            this.$message({
              type: 'success',
              message: '删除成功!',
            })
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    changePage() {
      // console.log(val)
      this.getList()
    },
    search() {
      this.getList(this.searchForm.keyword)
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/scss/common';
</style>
